# encoding:utf-8
# id         | integer                     | not null default nextval('contas_id_seq'::regclass)
# nome       | character varying(255)      | 
# descricao  | character varying(255)      | 
# usuario_id | integer                     | 
# created_at | timestamp without time zone | not null
# updated_at | timestamp without time zone | not null
# ativa      | boolean                     | default true
class Conta < ActiveRecord::Base
  attr_protected :usuario

  belongs_to :usuario
	has_many :transacoes, :order => "data asc, created_at asc", :dependent => :destroy
	has_many :creditos, :order => "data asc, created_at asc", :dependent => :destroy

  validates :usuario, :nome,
  	:presence => true
  
  #
  #
  def saldo
  	val = BigDecimal("0.00")
  	transacoes.each { |t| val += (t.valor * t.fator_transacao) }
  	
  	return val
  end
  
end
